﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace NetCoreClass
{
    public class textClear
    {

        /// <summary>
        /// include 的摘要说明。
        /// </summary>

        #region 字符串截取函数
        /// <summary>
        /// 字符串截取函数
        /// </summary>
        /// <param name="inputString">要截取的字符串</param>
        /// <param name="len">要截取的长度</param>
        /// <returns>string</returns>
        /// 
        public static string CutString(string inputString, int len)
        {


            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen > len)
                    break;
            }
            //   //如果截过则加上半个省略号 
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            if (mybyte.Length > len)
                tempString += "…";

            return tempString;
        }
        #endregion

        #region 生成由日期组成的唯一的文件名
        /// <summary>
        /// 生成由日期组成的唯一的文件名
        /// </summary>
        /// <returns>string</returns>
        /// 
        public  string makeFileName()
        {
            string newFileName;
            string dateName = System.DateTime.Now.ToString("yyyyMMddhhmmss");
            System.Random srd = new Random();
            int srdName = srd.Next(1000);
            newFileName = dateName + srdName.ToString();
            return newFileName;
        }
        #endregion

        #region 过滤特殊字符
        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="inputStr">字符串</param>
        /// <returns>string</returns>
        public  string cutBadStr(string inputStr)
        {
            //inputStr = inputStr.Replace(",", "");
            inputStr = inputStr.Replace("<", "&lt;");
            inputStr = inputStr.Replace(">", "&gt;");
            inputStr = inputStr.Replace("%", "");
            //inputStr = inputStr.Replace(".", "");
            inputStr = inputStr.Replace(":", "");
            inputStr = inputStr.Replace("#", "");
            inputStr = inputStr.Replace("&", "");
            inputStr = inputStr.Replace("$", "");
            inputStr = inputStr.Replace("^", "");
            inputStr = inputStr.Replace("*", "");
            inputStr = inputStr.Replace("`", "");
            inputStr = inputStr.Replace(" ", "");
            inputStr = inputStr.Replace("~", "");
            
            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@" or ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            inputStr = regex1.Replace(inputStr, "");
            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" and ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            inputStr = regex2.Replace(inputStr, "");
            return inputStr;

        }
        #endregion

        #region 过滤html标记
        /// <summary>
        /// 过滤html标记
        /// </summary>
        /// <param name="HTMLStr">要过滤的字符串</param>
        /// <returns>string</returns>
        ///
        public string clearHtml(string strhtml,int sum)
        {
            Regex ar = new Regex("<[^>]+>", RegexOptions.Compiled);
            string output = strhtml.Replace(" ", "");
            output = ar.Replace(output, "");
            //output = output.Replace("<", "");
            if (output.Length > sum)
            {
                output = output.Remove(sum);
            }
            return output;
        }
        public string CutHTML(string strHtml)
        {
            string[] aryReg ={   
          @"<script[^>]*?>.*?</script>",   
    @"<%",
    @"%>",
          @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",   
          @"([ ])[\s]+",   
          @"&(quot|#34);",   
          @"&(amp|#38);",   
          @"&(lt|#60);",   
          @"&(gt|#62);",     
          @"&(nbsp|#160);",     
          @"&(iexcl|#161);",   
          @"&(cent|#162);",   
          @"&(pound|#163);",   
          @"&(copy|#169);",   
          @"&#(\d+);",   
          @"-->",   
          @"<!--.* "  ,
 @"<iframe[\s\S]+</iframe *>",
 @"<frameset[\s\S]+</frameset *>",
 @"\r\n"
                             };

            string[] aryRep =   {   
             "",   
             "",
             "",
                          "",   
             "",   
             "\"",   
             "&",   
             "<",   
             ">",   
             "   ",   
             "\xa1",//chr(161),   
             "\xa2",//chr(162),   
             "\xa3",//chr(163),   
             "\xa9",//chr(169),   
             "",   
             " ",   
             "",
             "",
             "",
             ""
            };

            string newReg = aryReg[0];
            string strOutput = strHtml;
            for (int i = 0; i < aryReg.Length; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }
            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace(" ", "");
            return strOutput;
        }
        #endregion
        public string checkStr(string html)
        {

            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

          System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

         //   System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

           // System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

           // System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

          //  System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            

            html = regex1.Replace(html, ""); //过滤<script></script>标记

            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性

            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件

            html = regex4.Replace(html, ""); //过滤iframe 

            html = regex5.Replace(html, ""); //过滤frameset 

           // html = regex6.Replace(html, ""); //过滤frameset 

          //  html = regex7.Replace(html, ""); //过滤frameset 

           // html = regex8.Replace(html, ""); //过滤frameset 

          //  html = regex9.Replace(html, "");

           // html = html.Replace(" ", "");

           // html = html.Replace("</strong>", "");

           // html = html.Replace("<strong>", "");

           // html = cutBadStr(html);
            html = html.Replace("<%", "");
            html = html.Replace("%>", "");
            html = html.Replace("<?", "");
            html = html.Replace("?>", "");

            return html;

        }


        #region 标题固定长度


        /// <summary>
        /// <table style="font-size:12px">
        /// <tr><td><b>功能描述</b>：填充或截断原始字符串为指定长度 </td></tr>
        /// <tr><td><b>创 建 人</b>： </td></tr>
        /// <tr><td><b>创建时间</b>：</td></tr>
        /// </table>
        /// </summary>
        /// <param name="strOriginal">原始字符串</param>
        /// <param name="maxTrueLength">字符串的字节长度</param>
        /// <param name="chrPad">填充字符</param>
        /// <param name="blnCutTail">字符串的字节长度超过maxTrueLength时截断多余字符</param>
        /// <returns>填充或截断后的字符串</returns>
         public string PadRightTrueLen(string strOriginal, int maxTrueLength, char chrPad, bool blnCutTail)
        {
            string strNew = strOriginal;

            if (strOriginal == null || maxTrueLength <= 0)
            {
                strNew = "";
                return strNew;
            }

            int trueLen = TrueLength(strOriginal);
            if (trueLen > maxTrueLength)//超过maxTrueLength
            {
                if (blnCutTail)//截断
                {
                    for (int i = strOriginal.Length - 1; i > 0; i--)
                    {
                        strNew = strNew.Substring(0, i);
                        if (TrueLength(strNew) == maxTrueLength)
                            break;
                        else if (TrueLength(strNew) < maxTrueLength)
                        {
                            strNew += chrPad.ToString();
                            break;
                        }
                    }
                }
            }
            else//填充
            {
                for (int i = 0; i < maxTrueLength - trueLen; i++)
                {
                    strNew += chrPad.ToString();
                }
            }

            return strNew;
        }

        //主方法
        public  string CutStringTitle(string inputString, int i)
        {
            return PadRightTrueLen(inputString, i, ' ', true);
        }

        /// <summary>
        /// <table style="font-size:12px">
        /// <tr><td><b>功能描述</b>：字符串的字节长度 </td></tr>
        /// <tr><td><b>创 建 人</b>： </td></tr>
        /// <tr><td><b>创建时间</b>：</td></tr>
        /// </table>
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>字符串的字节长度</returns>
         public int TrueLength(string str)
        {
            int lenTotal = 0;
            int n = str.Length;
            string strWord = "";
            int asc;
            for (int i = 0; i < n; i++)
            {
                strWord = str.Substring(i, 1);
                asc = Convert.ToChar(strWord);
                if (asc < 0 || asc > 127)
                    lenTotal = lenTotal + 2;
                else
                    lenTotal = lenTotal + 1;
            }

            return lenTotal;
        }
        #endregion

     
        #region 打开没有Toolbar的新窗口(静态)

        /// <summary>
        /// 返回js脚本——window.open('','')
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="name">目标</param>
        /// <returns></returns>
        public  string openWeb(string url, string name)
        {
            //输出提示信息
            return "<script language=javascript>window.open('" + url + "','" + name + "')</script>";
        }

        
        #endregion


    }
}

 

